home *** CD-ROM | disk | FTP | other *** search
/ CICA 1993 April / CICA MS Windows - April 1993.iso / unzipped / programr / bcpp / bmploadr / bmploadr.c next >
C/C++ Source or Header  |  1992-03-14  |  16KB  |  464 lines

  1. /*
  2.    Windows 3.0 Bitmap Loader
  3.  
  4.    Version 1.00
  5.  
  6.    Started:    February 28, 1992
  7.    Finished:
  8.  
  9.    Developer : Jim Huang
  10. */
  11.  
  12. #include <windows.h>
  13. #include <stdlib.h>
  14. #include <direct.h>
  15. #include <time.h>
  16. #include <dos.h>
  17.  
  18. #include "bmploadr.h"
  19.  
  20.         /* Windows Function - Prototyping */
  21. long FAR PASCAL WndProc         (HWND, WORD, WORD, LONG);
  22. BOOL FAR PASCAL AboutDlgProc    (HWND, WORD, WORD, LONG);
  23. BOOL FAR PASCAL FileDlgProc     (HWND, WORD, WORD, LONG);
  24. BOOL FAR PASCAL SetupDlgProc     (HWND, WORD, WORD, LONG);
  25.  
  26. BOOL extern FAR PASCAL SetDeskWallPaper (LPSTR);
  27.  
  28. void SetNextBMP (LPSTR);
  29.  
  30. HANDLE hInst;                 /* Handle to Windows instances   */
  31. char   szAppName[] = "BMP",   /* Application Name              */
  32.        szFileName[96],
  33.        szCurDir[96],
  34.        Random[4], AutoLoad[4];
  35.  
  36. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  37.             LPSTR lpszCmdLine, int nCmdShow)
  38.     {
  39.     HWND     hwnd;       /* Handle to the main Window            */
  40.     MSG      msg;        /* MSG structure to store your messages */
  41.     WNDCLASS wndclass;   /* Windows class structure              */
  42.     HANDLE   hAccel;     /* Handle to the accelerator keys       */
  43.  
  44.     hInst = hInstance;   /* Handle for Windows instances     */
  45.  
  46.     /* Create the application window */
  47.     if (!hPrevInstance)
  48.        {
  49.        wndclass.style         = CS_HREDRAW | CS_VREDRAW | CS_BYTEALIGNWINDOW;
  50.        wndclass.lpfnWndProc   = WndProc;
  51.        wndclass.cbClsExtra    = 0;
  52.        wndclass.cbWndExtra    = 0;
  53.        wndclass.hInstance     = hInst;
  54.        wndclass.hIcon         = LoadIcon (hInstance, szAppName);
  55.        wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW);
  56.        wndclass.hbrBackground = GetStockObject (WHITE_BRUSH);
  57.        wndclass.lpszMenuName  = szAppName;
  58.        wndclass.lpszClassName = szAppName;
  59.  
  60.        RegisterClass (&wndclass);
  61.        }
  62.  
  63.  
  64.      /* Create the application windows */
  65.     hwnd = CreateWindow (szAppName, "Bitmap Loader",
  66.           WS_CAPTION      | WS_SYSMENU      |
  67.           WS_MINIMIZEBOX  | WS_CLIPCHILDREN | WS_OVERLAPPED,
  68.           0, 0, 190, 40, NULL, NULL, hInstance, NULL);
  69.  
  70.     ShowWindow (hwnd, nCmdShow);
  71.     UpdateWindow (hwnd);
  72.  
  73.     /* Load the accelerator table */
  74.     hAccel = LoadAccelerators (hInstance, szAppName);
  75.  
  76.     /* Message Handling Loop */
  77.     while (GetMessage (&msg, NULL, 0, 0))
  78.     {
  79.     if (!TranslateAccelerator (hwnd, hAccel, &msg))
  80.       {
  81.       TranslateMessage (&msg) ;
  82.       DispatchMessage (&msg) ;
  83.       }
  84.     }
  85.  
  86.     return msg.wParam;
  87. }
  88.  
  89. long FAR PASCAL WndProc (HWND hwnd, WORD message, WORD wParam, LONG lParam)
  90.     {
  91.     static HANDLE hInstance;    /* handle for each instances    */
  92.     static HCURSOR hCursor;     /* handle for the cursor        */
  93.     static HICON  hIcon;        /* handle for the icon          */
  94.  
  95.           /* Variables determining the position of the scroll bar */
  96.     FARPROC   lpfn;                     /* Far Pointer for each dialog box       */
  97.     HDC       hdc;                      /* handle for the display device         */
  98.     HMENU     hmenu;                    /* handle for the menu                   */
  99.     PAINTSTRUCT   ps;                   /* holds PAINT information               */
  100.     char      szBuffer[80];
  101.     short i;
  102.     switch (message)
  103.      {
  104.      case WM_CREATE:
  105.          /* The WM_CREATE message is sent once to a window when the */
  106.          /* window is created.  The window procedure for the new window */
  107.          /* receives this message after the window is created, but      */
  108.          /* before the window becomes visible.                          */
  109.          /*                                                             */
  110.          /* Parameters:                                                 */
  111.          /*                                                             */
  112.          /*    lParam  -  Points to a CREATESTRUCT structure with       */
  113.          /*               the following form:                           */
  114.          /*                                                             */
  115.          /*    typedef struct                                           */
  116.          /*              {                                              */
  117.          /*               LPSTR     lpCreateParams;                     */
  118.          /*               HANDLE    hInst;                              */
  119.          /*               HANDLE    hMenu;                              */
  120.          /*               HWND      hwndParent;                         */
  121.          /*               int       cy;                                 */
  122.          /*               int       cx;                                 */
  123.          /*               int       y;                                  */
  124.          /*               int       x;                                  */
  125.          /*               LONG      style;                              */
  126.          /*               LPSTR     lpszName;                           */
  127.          /*               LPSTR     lpszClass;                          */
  128.          /*               DWORD     dwExStyle;                          */
  129.          /*      }  CREATESTRUCT;                                       */
  130.  
  131.          hIcon = LoadIcon (hInstance, szAppName);
  132.          hInstance = ((LPCREATESTRUCT) lParam)->hInstance;
  133.  
  134.          GetProfileString (szAppName, "BMPSource", NULL, szCurDir, sizeof (szCurDir));
  135.          GetProfileString (szAppName, "Wallpaper", NULL, szFileName, sizeof (szFileName));
  136.          GetProfileString (szAppName, "AutoLoad", NULL, AutoLoad, sizeof (AutoLoad));
  137.          GetProfileString (szAppName, "Random", NULL, Random, sizeof (Random));
  138.  
  139.          if (lstrlen (szFileName)) {
  140.            lstrcpy (szBuffer, szCurDir);
  141.            lstrcat (szBuffer, "\\");
  142.            lstrcat (szBuffer, szFileName);
  143.            if (SetDeskWallPaper (szBuffer)) {
  144.              SetWindowText (hwnd, szFileName);
  145.              InvalidateRect (GetDesktopWindow(), NULL, TRUE);
  146.            }
  147.          }
  148.  
  149.          if (!lstrcmpi (Random, "On"))
  150.            SetNextBMP (szCurDir);
  151.  
  152.          if (!lstrlen (szCurDir))
  153.            SendMessage (hwnd, WM_COMMAND, IDM_SETUP, 0L);
  154.  
  155.          return 0;
  156.  
  157.      case WM_COMMAND:
  158.          /* The Windows messages for action bar and pulldown menu items */
  159.          /* are processed here.                                         */
  160.          /* The WM_COMMAND message contains the message ID in its first */
  161.          /* parameter (wParam). This routine is programmed to SWITCH on */
  162.          /* the #define values for the menu items in the application's  */
  163.          /* header (*.H) file. The ID values have the format, IDM_itemname. */
  164.          /* The service routines for the various menu items follow      */
  165.          /* the CASE statements up to the generated BREAK statements.   */
  166.  
  167.          /* Get the menu handle */
  168.          hmenu = GetMenu (hwnd);
  169.  
  170.          switch (wParam) {
  171.            case IDM_ABOUT:
  172.              lpfn = MakeProcInstance (AboutDlgProc, hInstance);
  173.              DialogBox (hInstance, "AboutDlg", hwnd, lpfn);
  174.              FreeProcInstance (lpfn);
  175.              return 0;
  176.  
  177.            case IDM_OPEN:
  178.              lpfn = MakeProcInstance (FileDlgProc, hInstance);
  179.              DialogBox (hInstance, "FileDlg", hwnd, lpfn);
  180.              FreeProcInstance (lpfn);
  181.  
  182.              SetWindowText (hwnd, szFileName);
  183.  
  184.              return 0;
  185.  
  186.            case IDM_SETUP:
  187.              lpfn = MakeProcInstance (SetupDlgProc, hInstance);
  188.              DialogBox (hInstance, "SetupDlg", hwnd, lpfn);
  189.              FreeProcInstance (lpfn);
  190.  
  191.              return 0;
  192.  
  193.            case IDM_EXIT:
  194.              SendMessage (hwnd, WM_CLOSE, 0, 0L);
  195.              return 0;
  196.          }
  197.          break;
  198.  
  199.      case WM_PAINT:
  200.      /* Obtain a handle to the device context                       */
  201.      /* BeginPaint will sends WM_ERASEBKGND if appropriate          */
  202.  
  203.        hdc = BeginPaint (hwnd, &ps);
  204.  
  205.        EndPaint (hwnd, &ps);
  206.  
  207.        return 0;
  208.  
  209.      /* Tell Window to close the application window.  Destroy the window */
  210.      case WM_CLOSE:
  211.        DestroyWindow (hwnd);
  212.        return 0;
  213.  
  214.          /* Destroys the window */
  215.      case WM_DESTROY:
  216.        PostQuitMessage (0);
  217.        return 0;
  218.      }
  219.      return DefWindowProc (hwnd, message, wParam, lParam);
  220. }
  221.  
  222.  
  223. /* ***************************************************************************** */
  224.  
  225. void SetNextBMP (LPSTR dir)
  226. {
  227.   char szBuffer[80];
  228.   short i = 0, nextbmp, j;
  229.   struct _find_t findt;
  230.  
  231.   lstrcpy (szBuffer, dir);
  232.   lstrcat (szBuffer, "\\");
  233.   lstrcat (szBuffer, "*.BMP");
  234.   srand ((unsigned)time(NULL));
  235.   nextbmp = rand();
  236.  
  237.   if (!_dos_findfirst (szBuffer, _A_NORMAL, &findt)) {
  238.     i++;
  239.     while (!_dos_findnext (&findt))
  240.       i++;
  241.  
  242.     _dos_findfirst (szBuffer, _A_NORMAL, &findt);
  243.     if (i == 0)
  244.        WriteProfileString (szAppName, "Wallpaper", findt.name);
  245.     else {
  246.        for (j = 0; j < nextbmp % i; j++)
  247.          _dos_findnext (&findt);
  248.        WriteProfileString (szAppName, "Wallpaper", findt.name);
  249.     }
  250.   }
  251. }
  252.  
  253.  
  254. LPSTR lstrchr (LPSTR str, char ch)
  255. {
  256.   while (*str)
  257.     {
  258.     if (ch == *str)
  259.     return str;
  260.  
  261.     str = AnsiNext (str);
  262.     }
  263.  
  264.   return NULL;
  265. }
  266.  
  267. LPSTR lstrrchr (LPSTR str, char ch)
  268. {
  269.   LPSTR strl = str + lstrlen (str);
  270.  
  271.   do {
  272.     if (ch == *strl)
  273.     return strl;
  274.  
  275.     strl = AnsiPrev (str, strl);
  276.   } while (strl > str);
  277.  
  278.   return NULL;
  279. }
  280.  
  281.  
  282. /* ***************************************************************************** */
  283.  
  284.  
  285. BOOL FAR PASCAL AboutDlgProc (HWND hDlg, WORD message, WORD wParam, LONG lParam)
  286. {
  287.    switch (message)
  288.      {
  289.      case WM_INITDIALOG:
  290.        SetDlgItemText (hDlg, IDD_VER, "Version 1.00a");
  291.        SetDlgItemText (hDlg, IDD_NAME, "Written by: Jim Huang");
  292.        SetDlgItemText (hDlg, IDD_DATE, "Copyright ⌐1992");
  293.  
  294.        return TRUE;
  295.  
  296.      case WM_COMMAND:
  297.        switch (wParam)
  298.          {
  299.          case IDOK:
  300.            EndDialog (hDlg, 0);
  301.            return TRUE;
  302.          }
  303.        break;
  304.      }
  305.    return FALSE;
  306. }
  307.  
  308.  
  309. BOOL FAR PASCAL FileDlgProc (HWND hDlg, WORD message, WORD wParam, LONG lParam)
  310. {
  311.     static char cLastChar, szFName[96] = "*.BMP", szFileSpec[16];
  312.     char szBuffer[96];
  313.     short nEditLen, count;
  314.     WORD  wFileAttr;
  315.  
  316.     switch (message)
  317.        {
  318.        case WM_INITDIALOG:
  319.          lstrcpy (szBuffer, szCurDir);
  320.          SendDlgItemMessage (hDlg, IDD_FNAME, EM_LIMITTEXT, 80, 0L) ;
  321.          DlgDirList (hDlg, szBuffer, IDD_DLIST, IDD_FPATH, 0x0010 | 0x4000 | 0x8000);
  322.          DlgDirList (hDlg, "*.BMP", IDD_FLIST, IDD_FPATH, 0);
  323.          SetDlgItemText (hDlg, IDD_FNAME, szFName);
  324.          return TRUE;
  325.  
  326.        case WM_COMMAND:
  327.          switch (wParam)
  328.            {
  329.            case IDD_FLIST:
  330.              switch (HIWORD (lParam))
  331.                {
  332.                case LBN_SELCHANGE:
  333.                  if (DlgDirSelect (hDlg, szFileName, IDD_FLIST))
  334.                    lstrcat (szFileName, szFileSpec);
  335.                  SetDlgItemText (hDlg, IDD_FNAME, szFileName);
  336.                  return TRUE;
  337.  
  338.                case LBN_DBLCLK:
  339.                  if (DlgDirSelect (hDlg, szFileName, IDD_FLIST)) {
  340.                    lstrcat (szFileName, szFileSpec);
  341.                    DlgDirList (hDlg, "*.BMP", IDD_FLIST, IDD_FPATH, 0);
  342.                    SetDlgItemText (hDlg, IDD_FNAME, szFileSpec);
  343.                  }
  344.                  else {
  345.                    SetDlgItemText (hDlg, IDD_FNAME, szFileName);
  346.                    SendMessage (hDlg, WM_COMMAND, IDOK, 0L);
  347.                  }
  348.                  return TRUE ;
  349.                }
  350.              break;
  351.  
  352.            case IDD_DLIST:
  353.              switch (HIWORD (lParam))
  354.                {
  355.                case LBN_DBLCLK:
  356.                  if (DlgDirSelect (hDlg, szFileName, IDD_DLIST)) {
  357.                    lstrcat (szFileName, szFileSpec);
  358.                    DlgDirList (hDlg, szFileName, IDD_DLIST, IDD_FPATH, 0x0010 | 0x4000 | 0x8000);
  359.                    DlgDirList (hDlg, "*.BMP", IDD_FLIST, IDD_FPATH, 0);
  360.                  }
  361.                  return TRUE ;
  362.                }
  363.              break;
  364.  
  365.            case IDD_FNAME:
  366.              if (HIWORD (lParam) == EN_CHANGE)
  367.                 EnableWindow (GetDlgItem (hDlg, IDOK), (BOOL) SendMessage (LOWORD (lParam),
  368.                               WM_GETTEXTLENGTH, 0, 0L));
  369.              return TRUE;
  370.  
  371.            case IDOK:
  372.              GetDlgItemText (hDlg, IDD_FNAME, szFileName, 80);
  373.  
  374.              nEditLen = lstrlen (szFileName);
  375.              cLastChar = *AnsiPrev (szFileName, szFileName + nEditLen);
  376.  
  377.              if (cLastChar == '\\' || cLastChar == ':')
  378.                lstrcat (szFileName, szFileSpec);
  379.  
  380.              if (lstrchr (szFileName, '*') || lstrchr (szFileName, '?')) {
  381.                if (DlgDirList (hDlg, szFileName, IDD_FLIST, IDD_FPATH, wFileAttr)) {
  382.                  lstrcpy (szFileSpec, szFileName);
  383.                  SetDlgItemText (hDlg, IDD_FNAME, szFileSpec);
  384.                }
  385.                else
  386.                  MessageBeep (0);
  387.  
  388.                return TRUE;
  389.              }
  390.  
  391.              lstrcat (lstrcat (szFileName, "\\"), szFileSpec);
  392.  
  393.              if (DlgDirList (hDlg, szFileName, IDD_FLIST, IDD_FPATH, wFileAttr)) {
  394.                lstrcpy (szFileSpec, szFileName);
  395.                SetDlgItemText (hDlg, IDD_FNAME, szFileSpec);
  396.                return TRUE;
  397.              }
  398.  
  399.              szFileName[nEditLen] = '\0';
  400.  
  401.              if (SetDeskWallPaper (szFileName)) {
  402.                WriteProfileString (szAppName, "Wallpaper", szFileName);
  403.                WriteProfileString ("windows", "Wallpaper", "(None)");
  404.                InvalidateRect (GetDesktopWindow(), NULL, TRUE);
  405.              }
  406.  
  407.            case IDCANCEL :
  408.              EndDialog (hDlg, FALSE);
  409.              return TRUE;
  410.            }
  411.        }
  412.  
  413.     return FALSE;
  414. }
  415.  
  416. BOOL FAR PASCAL SetupDlgProc (HWND hDlg, WORD message, WORD wParam, LONG lParam)
  417. {
  418.    switch (message)
  419.      {
  420.      case WM_INITDIALOG:
  421.           if (lstrlen (szCurDir))
  422.             SetDlgItemText (hDlg, IDD_DIRECTORY, szCurDir);
  423.  
  424.           if (!lstrcmpi (AutoLoad, "On"))
  425.             CheckDlgButton (hDlg, IDD_CHECK1, 1);
  426.  
  427.           if (!lstrcmpi (Random, "On"))
  428.             CheckDlgButton (hDlg, IDD_CHECK2, 1);
  429.  
  430.        return TRUE;
  431.  
  432.      case WM_COMMAND:
  433.        switch (wParam)
  434.          {
  435.          case IDOK:
  436.            GetDlgItemText (hDlg, IDD_DIRECTORY, szCurDir, sizeof (szCurDir));
  437.  
  438.            if (!lstrlen (szCurDir)) {
  439.              MessageBox (hDlg, "Please Specify the BitMap Directory", NULL, MB_OK | MB_ICONSTOP);
  440.              return FALSE;
  441.            }
  442.  
  443.            WriteProfileString (szAppName, "BMPSource", szCurDir);
  444.  
  445.            if (IsDlgButtonChecked (hDlg, IDD_CHECK1))
  446.              WriteProfileString (szAppName, "AutoLoad", "On");
  447.            else
  448.              WriteProfileString (szAppName, "AutoLoad", "Off");
  449.  
  450.            if (IsDlgButtonChecked (hDlg, IDD_CHECK2))
  451.              WriteProfileString (szAppName, "Random", "On");
  452.            else
  453.              WriteProfileString (szAppName, "Random", "Off");
  454.  
  455.          case IDCANCEL:
  456.            EndDialog (hDlg, 0);
  457.            return TRUE;
  458.          }
  459.        break;
  460.      }
  461.    return FALSE;
  462. }
  463.  
  464.